<?php
namespace api\modules\h5\controllers\comment;

use backend\widgets\provinces\Provinces;
use common\models\cart\Cart;
use common\models\goods\Goods;
use yii\data\ActiveDataProvider;
use Yii;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;
use common\models\store\Store;

/**
 * 商品评论接口
 *
 * Class InfoController
 * @package api\modules\h5\controllers\comment
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\comment\Comment';

    /**
     * 列表
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $goods_id = $request->get('goods_id')??0;

        if(!$goods_id)
            return ResultDataHelper::api(422, '商品ID不能为空');

        $query = $this->modelClass::find()
            ->where(['member_id'=>Yii::$app->user->identity->member_id])
            ->andWhere(['goods_id'=>$goods_id])
            ->select([])
            ->with(['member'])
            ->asArray();

        return new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Yii::$app->params['user.pageSize'],
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }

    /**
     * 添加评论
     * @return mixed|\yii\db\ActiveRecord
     * @throws NotFoundHttpException
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;

        //TODO:判断是否干嘛过商品

        return parent::actionCreate();
    }


    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['view']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
